<template>
  <div>
    <MyTable :arr="list">
      <template #head>
        <th>#</th>
        <th>商品名称</th>
        <th>价格</th>
        <th>标签</th>
        <th>操作</th>
      </template>
      <!--#body="{row}"  v-slot:body v-slot="{row}"  因为template里面不能同时有两个v-slot-->
      <template #body="{ row }">
        <td>{{ row.id }}</td>
        <td>{{ row.goods_name }}</td>
        <td>{{ row.goods_price }}</td>
        <td>
          <input
            v-focus
            class="tag-input form-control"
            style="width: 80px"
            type="text"
            v-if="row.inputVisible"
            @blur="row.inputVisible = false"
            v-model="row.inputValue"
            @keydown.esc="row.inputValue = ''"
            @keydown.enter="enterFn(row)"
          />
          <button
            v-else
            style="display: block"
            class="btn btn-primary btn-sm add-tag"
            @click="row.inputVisible = true"
          >
            +Tag
          </button>
          <!-- <span v-for="(item, index) in row.tags" :key="index">{{ item }}</span> -->
          <el-tag
            type="warning"
            v-for="(item, index) in row.tags"
            :key="index"
            >{{ item }}</el-tag
          >
        </td>
        <td>
          <button
            type="button"
            class="btn btn-large btn-block btn-danger"
            @click="delFn(row.id)"
          >
            删除
          </button>
        </td>
      </template>
    </MyTable>
  </div>
</template>

<script>
import MyTable from './MyTable'
export default {
  components: {
    MyTable,
  },
  data() {
    return {
      list: [], // 所有数据
    }
  },
  created() {
    this.$axios({
      url: '/api/goods',
    }).then((res) => {
      this.list = res.data.data
    })
  },
  methods: {
    delFn(id) {
      // console.log(id);
      this.list = this.list.filter((item) => item.id != id)
    },
    enterFn(row) {
      if (row.inputValue.trim() === '') return alert('请输入内容')
      row.tags.push(row.inputValue)
      row.inputValue = ''
      row.inputVisible = false
    },
  },
  //局部自定义指令
  directives: {
    focus: {
      inserted(el) {
        el.focus()
      },
    },
  },
}
</script>
<style>
.el-tag {
  margin-right: 5px;
}
</style>
